require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'bootstrap': '/js/lib/bootstrap.min',
        'datetimepicker': '/js/lib/bootstrap-datetimepicker.min',
        'select2': '/js/lib/select2.min',
        'paginator': '/js/lib/bootstrap-paginator'
    },
    shim: {
        'bootstrap': ['jquery'],
        'paginator': ['bootstrap']
    }
});

require(['jquery', 'doT', 'common', 'bootstrap', 'paginator'], function($, doT, common) {
    $(function() {
        var role = sessionStorage.getItem('role');
        var school_id = location.search.slice(1).split('=')[1];
        var provinceSelect = $('#provinceSelect');
        var citySelect = $('#citySelect');
        var districtSelect = $('#districtSelect');
        var provinces, citys, districts;

        if (role == 1) {
            $('.managerOnly').removeClass('managerOnly');
        }

        if (role == 2) {
            $('input').attr('disabled','disabled');
            $('select').attr('disabled','disabled');
        }

        function refeshProvinceSelect(school_detail) {
            $.getJSON(common.urlBase + '/location/province.json', function(result) {
                provinces = result.data;
                provinceSelect.html('');
                for (var i = 0; i < provinces.length; i++) {
                    $('<option/>', { value: provinces[i].id }).html(provinces[i].name).appendTo(provinceSelect);
                }
                if (school_detail) {
                    provinceSelect.val(school_detail.province_id);
                }
                refreshCitySelect(school_detail);
            });
        }

        function refreshCitySelect(school_detail) {
            citys = null;
            $.getJSON(common.urlBase + '/location/province-city/' + provinceSelect.val() + '.json', function(result) {
                citys = result.data;
                citySelect.html('');
                if (citys) {
                    for (var i = 0; i < citys.length; i++) {
                        $('<option/>', { value: citys[i].id }).html(citys[i].name).appendTo(citySelect);
                    }
                    if (school_detail) {
                        citySelect.val(school_detail.city_id);
                    }
                    refreshDistrictSelect(school_detail);
                }
            });
        }

        function refreshDistrictSelect(school_detail) {
            districts = null;
            $.getJSON(common.urlBase + '/location/city-area/' + citySelect.val() + '.json', function(result) {
                districts = result.data;
                districtSelect.html('');
                if (districts) {
                    for (var i = 0; i < districts.length; i++) {
                        $('<option/>', { value: districts[i].id }).html(districts[i].name).appendTo(districtSelect);
                    }
                }
                if (school_detail) {
                    districtSelect.val(school_detail.area_id);
                }
            });
        }

        provinceSelect.on('change', function(e) {
            refreshCitySelect();
        });
        citySelect.on('change', function(e) {
            refreshDistrictSelect();
        });

        function getSchoolDetail() {

            $.getJSON(common.urlBase + '/school/id-school/' + school_id + '.json', function(result) {
                var school_detail = result.data;

                refeshProvinceSelect(school_detail);

                $('#school_name').val(school_detail.school_name);
                $('#school_address').val(school_detail.school_address);
                $('#school_longtitude').val(school_detail.coord_lng);
                $('#school_altitude').val(school_detail.coord_lat);
                $('#app_url').val(school_detail.url);
                $('#app_secret').val(school_detail.appsecret);
                $('#app_id').val(school_detail.appid);
                $('#master_id').val(school_detail.master_id);
            });
        }

        getSchoolDetail();


        $('#editSchool').on('click', function(event) {
            var error = false;
            var form = $('#schoolForm');
            var input = form.find(':input[required]');
            input.each(function() {
                if (!$(this).val()) {
                    $('#noticeMsg').html('请输入正确的信息！');
                    $('#noticeModal').modal('show');
                    error = true;
                }
            });
            if (!error) {
                // var schoolData = $('#schoolForm').serialize();
                // schoolData = 'school_id=' + school_id + '&' + schoolData;
                var schoolData = {
                    school_id: school_id,
                    province_id: $('#provinceSelect').val(),
                    city_id: $('#citySelect').val(),
                    area_id: $('#districtSelect').val(),
                    school_name: $('#school_name').val(),
                    school_address: $('#school_address').val(),
                    coord_lng: $('#school_longtitude').val(),
                    coord_lat: $('#school_altitude').val(),
                    appid: $('#app_id').val(),
                    appsecret: $('#app_secret').val(),
                    url: $('#app_url').val(),
                    master_id: $('#master_id').val()
                };
                $.post(common.urlBase + '/school/editSchool/edit.json', schoolData, function(result) {
                    if (result.ok) {
                        $('#noticeMsg').html('修改学校信息成功!');
                        $('#noticeModal').modal('show');
                    }
                }, 'json');
            }
        });
    });
});
